import 'package:flutter/foundation.dart';
import 'package:flutter_learn_vi/src/bean/bean_mine.dart';
import 'package:flutter_learn_vi/src/bean/bean_user.dart';
import 'package:flutter_learn_vi/src/common/user_helper.dart';
import 'package:flutter_learn_vi/src/net/dio_utils.dart';
import 'package:flutter_learn_vi/src/utils/log_util.dart';
import 'package:flutter_learn_vi/src/utils/string_utils.dart';

import '../common/sp_key.dart';
import '../utils/sp_utils.dart';

/// 创建人： Created by zhaolong
/// 创建时间：Created by  on 2021/1/17.
///
/// 可关注公众号：我的大前端生涯   获取最新技术分享
/// 可关注网易云课堂：https://study.163.com/instructor/1021406098.htm
/// 可关注博客：https://blog.csdn.net/zl18603543572
///
/// 代码清单
///代码清单
class UserModel {
  ///检查用户状态
  Future<bool> checkUserInfo(int userId) async {
    ResponseInfo responseInfo = await DioUtils.instance.getRequest(
      url: HttpHelper.checkUserInfo,
    );
    if (responseInfo.success) {
      return true;
    }
    String message = responseInfo.message;
    return false;
  }

  ///刷新当前登录用户的数据
  Future<bool> getUserInfo() async {
    //post请求发送
    ResponseInfo responseInfo = await DioUtils.instance.getRequest(
      url: HttpHelper.userInfo,
      responseEncrypt: true,
    );
    UserBean userBean;
    //登录成功
    if (responseInfo.success && responseInfo.data != null) {
      userBean = UserBean.fromJson(responseInfo.data);
      UserHelper.getInstance.setUserBean(userBean);
    } else {
      SPUtil.remove(spUserBeanKey);
      LogUtils.e("获取用户信息失败");
    }
    if (userBean == null) {
      return false;
    }
    ResponseInfo mineResponse = await DioUtils.instance.getRequest(
      url: HttpHelper.sysmineimg + userBean.mineId.toString(),
    );
    //矿成功
    if (mineResponse.success) {
      MineBean mineBean = MineBean.fromJson(mineResponse.data);
      UserHelper.getInstance.mineBean = mineBean;
    } else {
      LogUtils.e("获取矿信息失败");
    }

    return true;
  }

  Future<List<MineBeanDepartmentBean>> getMineDepartmentRequest(
      {int mineId}) async {
    ResponseInfo mineResponse = await DioUtils.instance.postRequest(
        url: HttpHelper.sysmineDepartment,
        encrypt: true,
        jsonMap: {"mineId": mineId});
    List<MineBeanDepartmentBean> departmentList = [];
    //矿成功
    if (mineResponse.success) {
      List list = mineResponse.data;
      if (list != null) {
        list.forEach((element) {
          departmentList.add(MineBeanDepartmentBean.fromJson(element));
        });
      }
    } else {
      LogUtils.e("获取矿信息失败");
    }
    return departmentList;
  }

  ///获取矿长部门用户信息
  Future<List<UserBean>> getMineDepartmentUserRequest(
      {@required int mineId,
      @required String department,
      int pageIndex = 1,
      String userName}) async {
    Map<String, dynamic> map = {"department": department, "mineId": mineId};
    if (!StringUtils.isEmpty(userName)) {
      map["reamName"] = userName;
    }
    map["page"] = pageIndex;
    ResponseInfo mineResponse = await DioUtils.instance.postRequest(
        url: HttpHelper.sysmineDepartmentUserUrl, encrypt: true, jsonMap: map);
    List<UserBean> userList = [];
    //矿成功
    if (mineResponse.success) {
      var page = mineResponse.data;
      List list = page["list"];
      if (list != null) {
        list.forEach((element) {
          userList.add(UserBean.fromJson(element));
        });
      }
    } else {
      LogUtils.e("获取矿信息失败");
    }
    return userList;
  }
}
